/**
 * Copyright 2010 Kurtis Nusbaum
 *
 * This file is part of RemoteRunner.  
 *
 * RemoteRunner is free software: you can 
 * redistribute it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation, either version 2 of the 
 * License, or (at your option) any later version.  
 *
 * RemoteRunner is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.  You should have received a copy of the GNU  General 
 * Public License along with RemoteRunner. If not, see 
 * http://www.gnu.org/licenses/.
 */

package org.klnusbaum.remoterunner;

import android.app.AlertDialog;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.text.TextWatcher;
import android.content.DialogInterface;
import android.text.Editable;
import android.os.Bundle;
import android.content.Context;
import android.util.Log;

/**
 * Class for editing new or existing hosts.
 */
public class EditHostDialog extends AlertDialog implements TextWatcher{

	/** EditText containing the name of the host. */
  private EditText hostnameEdit;
	/** EditText containing the username for logging into the host. */
  private EditText usernameEdit;
	/** EditText containing the name used for connecting to the host. */
  private EditText nameEdit;

	/** Constants used for saving state */
	private static final String EXTRA_HOSTNAME="HOSTNAME";
	private static final String EXTRA_USERNAME="USERNAME";
	private static final String EXTRA_NAME="NAME";
	private static final String EXTRA_MY_STATE="MY_STATE";


	/** If we're editing an existing host, this is it's ID number.
		Otherwise is set to HostsDB.INVALID_HOST_ID */
	private int hostID;

	/** The dataabse containing host information */
	private HostsDB hostsDatabase;

	/** Listner to be used for the Ok and Cancel buttons */
	private final DialogInterface.OnClickListener buttonListener;

	static final int BUTTON_OK = DialogInterface.BUTTON_POSITIVE;

	/**
   * Constructs an EditHostDialog. Use this constructor when editing
	 * an existing Host.
   *
	 * @param context The context in which the dialog is being created.
	 * @param buttonListener Listener to be used for the Ok and Cancel buttons.
	 * @param hostID The ID of the host that is being edited. 
		 @param hostsDatabase The database containing all host information.
	 */
	public EditHostDialog(Context context, 
		DialogInterface.OnClickListener buttonListener, 
		DialogInterface.OnDismissListener onDismissListener,
		int hostID,
		HostsDB hostsDatabase)
	{
		super(context);
		this.buttonListener = buttonListener;
		init(hostID, onDismissListener, hostsDatabase);
	}

	/**
   * Constructs an EditHostDialog. Use this constructor when adding
	 * a new Host.
   *
	 * @param context The context in which the dialog is being created.
	 * @param buttonListener Listener to be used for the Ok and Cancel buttons.
		 @param hostsDatabase The database containing all host information.
	 */
	public EditHostDialog(Context context, 
		DialogInterface.OnClickListener buttonListener, 
		DialogInterface.OnDismissListener onDismissListener,
		HostsDB hostsDatabase)
	{
		super(context);
		this.buttonListener = buttonListener;
		init(HostsDB.INVALID_HOST_ID, onDismissListener, hostsDatabase);
	}

	/**
   * Initializes the dialog with the given attributes.
	 *
	 * @param buttonListener The listener to be used for the Ok and Cancel button.
	 * @param hostID The ID of the host being edited. If no host is being edited
	 * and instead a new one is being added, this value should be set to
	 * HostsDB.INVALID_HOST_ID.
		 @param hostsDatabase The database containing all host information.
	 */
	private void init(int hostID, 
		DialogInterface.OnDismissListener onDismissListener,
		HostsDB hostsDatabase)
	{
		setOnDismissListener(onDismissListener);
		this.hostID = hostID;
		this.hostsDatabase = hostsDatabase;
	}

	@Override
  public void onCreate(Bundle savedInstanceState){
		View view = getLayoutInflater().inflate(R.layout.edit_host_layout, null);
    setView(view);

    hostnameEdit = (EditText) view.findViewById(R.id.hostnameEdit);
    usernameEdit = (EditText) view.findViewById(R.id.usernameEdit);
    nameEdit = (EditText) view.findViewById(R.id.nameEdit);

		if(isEditing()){
    	setTitle(R.string.edit_host);
			hostnameEdit.setText(hostsDatabase.getHostname(hostID));
			usernameEdit.setText(hostsDatabase.getUsername(hostID));
			nameEdit.setText(hostsDatabase.getName(hostID));
		}
		else{
			setTitle(R.string.add_host);
		}

		hostnameEdit.addTextChangedListener(this);
		usernameEdit.addTextChangedListener(this);
		nameEdit.addTextChangedListener(this);

		setButton(
			BUTTON_OK, 
			getContext().getString(android.R.string.ok),
			buttonListener);
		setButton(
			BUTTON_NEGATIVE, 
			getContext().getString(android.R.string.cancel), 
			buttonListener);
    super.onCreate(savedInstanceState);
		validate();
  }

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count){
	
	}

	@Override
	public void beforeTextChanged(
		CharSequence s, int start, int count, int after)
	{
	
	}

	@Override
	public void afterTextChanged(Editable editable){
		validate();
	}

	@Override
	public Bundle onSaveInstanceState(){
		Bundle toReturn = super.onSaveInstanceState();
		Bundle state = new Bundle();
		state.putCharSequence(EXTRA_HOSTNAME, hostnameEdit.getText());
		state.putCharSequence(EXTRA_USERNAME, usernameEdit.getText());
		state.putCharSequence(EXTRA_NAME, nameEdit.getText());

		toReturn.putBundle(EXTRA_MY_STATE, state);
		return toReturn;
	}

	@Override
	public void onRestoreInstanceState(Bundle state){
		super.onRestoreInstanceState(state);
		Bundle myState = state.getBundle(EXTRA_MY_STATE);
		hostnameEdit.setText(myState.getCharSequence(EXTRA_HOSTNAME));
		usernameEdit.setText(myState.getCharSequence(EXTRA_USERNAME));
		nameEdit.setText(myState.getCharSequence(EXTRA_NAME));
		validate();
	}

	/** 
	 * Validates whether or not the current inputs the user has specied 
   * are valid. If they aren't the Ok button is disabled.
   */
	private void validate(){
		if(
			!hostnameEdit.getText().toString().equals("") &&
      !usernameEdit.getText().toString().equals("") &&
      !nameEdit.getText().toString().equals("")
		)
		{
			getButton(BUTTON_OK).setEnabled(true);
		}
		else{
			getButton(BUTTON_OK).setEnabled(false);
		}
	}	
	
	/**
	 * Get the host name the user has entered.
	 */
	public String getHostname(){
		return hostnameEdit.getText().toString();
	}

	/**
	 * Get the username the user has specified.
	 */
	public String getUsername(){
		return usernameEdit.getText().toString();
	}

	/**
	 * Get the name the user entered.
	 */
	public String getName(){
		return nameEdit.getText().toString();
	}

	/**
	 * Get the ID of the host currently being edited.
	 * 
	 * @return The ID of the host currently being edited. If
	 * no host is being edited (just added) the this will return
	 * HostsDB.INVALID_HOST_ID.
	 */
	public int getHostID(){
		return hostID;
	}

	/**
	 * Whether or not we're editing a host or just adding a new one.
	 */
	public boolean isEditing(){
		return hostID != HostsDB.INVALID_HOST_ID;
	}

}
